const db = require('../backend/config/db');

async function testPermissions() {
  try {
    console.log('Testing database connection and permissions...');
    
    // Test SELECT permission
    const selectResult = await db.query('SELECT COUNT(*) FROM users');
    console.log('✓ SELECT permission OK');
    
    // Test INSERT permission
    const insertResult = await db.query(
      'INSERT INTO categories (user_id, name, type, icon, color, is_default) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id',
      [1, 'Test Category', 'expense', 'test', '#000000', false]
    );
    console.log('✓ INSERT permission OK');
    
    // Get the inserted category ID
    const categoryId = insertResult.rows[0].id;
    
    // Test UPDATE permission
    await db.query(
      'UPDATE categories SET name = $1 WHERE id = $2',
      ['Updated Test Category', categoryId]
    );
    console.log('✓ UPDATE permission OK');
    
    // Test DELETE permission
    await db.query('DELETE FROM categories WHERE id = $1', [categoryId]);
    console.log('✓ DELETE permission OK');
    
    console.log('All database permissions are working correctly!');
  } catch (error) {
    console.error('Database permission error:', error.message);
  }
}

testPermissions();